<template>
  <div class="bg">
    <form action="" class="form">
      <h4>武汉外卖管理系统</h4>

      <el-form
        :model="loginForm"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
        size="mini"
      >
        <el-form-item prop="account">
          <el-input
            type="text"
            v-model="loginForm.account"
            autocomplete="off"
            prefix-icon="iconfont icon-yonghu"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :type="flag ? 'password' : 'text'"
            v-model="loginForm.password"
            autocomplete="off"
            prefix-icon="iconfont icon-mima"
            @keyup.native.enter="submitForm('ruleForm')"
          >
            <!-- 以具名插槽的方式添加小眼睛,为了绑定事件 -->
            <template #suffix>
              <i
                class="iconfont"
                :class="flag ? 'icon-eyes' : 'icon-eyes-'"
                @click="flag = !flag"
              ></i>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            @click="submitForm('ruleForm')"
            >登 录</el-button
          >
        </el-form-item>
      </el-form>
    </form>
  </div>
</template>

<script>
import { validateName, validatePwd } from '../utils/validate.js'

// 引入登录功能函数
import { login } from '../api/users.js'

//引入本地存储
import local from '../utils/local.js'

//引入路由
import { calcRoutes } from '../router/index'
export default {
  data() {
    return {
      //获取输入框的值,使用到了双向数据绑定
      loginForm: {
        account: '',
        password: ''
      },
      //规则验证
      rules: {
        password: [{ validator: validatePwd, trigger: 'blur' }],
        account: [{ validator: validateName, trigger: 'blur' }]
      },
      flag: true,
      loading: false
    }
  },
  // 方法
  methods: {
    submitForm(formName) {
      this.loading = true
      this.$refs[formName].validate(async valid => {
        if (valid) {
          // alert('验证通过!')
          //这里验证通过就可以发请求了
          const { code, msg, token, role } = await login(this.loginForm)
          if (code === 0) {
            this.$message.success(msg)
            setTimeout(() => {
              this.$router.push('/')
            }, 2000)
            //拿到token向本地存储
            local.set('token', token)
            local.set('role', role)
            calcRoutes()
          } else {
            this.$message.error(msg)
            setTimeout(() => {
              this.loading = false
            }, 1000)
          }
          // console.log(data)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    fun() {
      console.log('发货单撒可富')
    }
  }
}
</script>

<style lang="less" scoped>
.bg {
  width: 100%;
  height: 100%;
  // background-color: #2b3d4b;
  background-image: url('../assets/images/美女.png');
  background-size: 100%;
  background-repeat: no-repeat;
  display: flex;
  justify-content: center;
  align-items: center;
  h4 {
    letter-spacing: 2px;
    text-align: center;
    color: black;
    font-weight: normal;
    font-size: 16px;
  }
  .el-form {
    padding-top: 10px;
    .el-button {
      width: 100%;
    }
  }
}
.form {
  width: 400px;
}
</style>
